local guowu = fk.CreateSkill {

  name = "joy__guowu",

  tags = { Skill.Compulsory, },

}



guowu:addEffect(fk.EventPhaseStart, {
  name = "joy__guowu",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(guowu.name) and player.phase == Player.Play and not player:isKongcheng()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player.player_cards[Player.Hand]
    player:showCards(cards)
    local types = {}
    for _, id in ipairs(cards) do
      table.insertIfNeed(types, Fk:getCardById(id).type)
    end
    if #types > 1 then
      room:addPlayerMark(player, "joy__guowu2-phase", 1)
    end
    if #types > 2 then
      room:addPlayerMark(player, "joy__guowu3-phase", 1)
    end
    cards = {}
    if #room.discard_pile <= 0 then return end
    for _, id in ipairs(room.discard_pile) do
        if #cards >= 1 then break end
        local card = Fk:getCardById(id)
        if card:isCommonTrick() then
            table.insertIfNeed(cards,id)
        end
        if #cards >= 1 then break end
    end
    if #cards > 0 then
        room:moveCards({
          ids = cards,
          to = player.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = guowu.name,
        })
    end
  end,
})

guowu:addEffect(fk.CardUseFinished, {
  name = "#joy__guowu_delay",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.CardUseFinished},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:getMark("joy__guowu3-phase") > 0 and not player.dead and
      (data.card:isCommonTrick() or data.card.trueName == "slash") then
        local n=0
        player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function (e)
          local use = e.data
          if use.from and use.from==player and (use.card:isCommonTrick() or use.card.trueName == "slash")  then
            n=n+1
          end
          return false
        end, Player.HistoryGame)
        return n == 1 and not player:prohibitUse(Fk:cloneCard(data.card.name))
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player,guowu.name,nil,"帼武：是否令【"..Fk:translate(data.card.name).."】额外执行一次效果？")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = Fk:cloneCard(data.card.name)
    local use ={
      from = player.id,
      tos = data.tos,
      card = name,
      extraUse = true
    }
    room:useCard(use)
  end,
})

guowu:addEffect("targetmod", {
  name = "#joy__guowu_targetmod",
  bypass_times =  function(self, player)
    return player:getMark("joy__guowu2-phase") > 0
  end,
})

return guowu